package com.sam.im.heiqun.uis.adapters;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.ImFriendEntivity;
import com.sam.im.heiqun.entities.ImMessage;
import com.sam.im.heiqun.entities.RedPacketEntivity;
import com.sam.im.heiqun.entities.TransferEntivity;
import com.sam.im.heiqun.uis.activities.ImageViewActivity;
import com.sam.im.heiqun.uis.widgets.BubbleImageView;
import com.sam.im.heiqun.uis.widgets.CustomShapeTransformation;
import com.sam.im.heiqun.uis.widgets.GifTextView;
import com.sam.im.heiqun.uis.widgets.MediaManager;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.google.gson.Gson;
import com.yuyh.library.utils.DimenUtils;
import com.yuyh.library.utils.GlideUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @desc发送消息的adapter
 * @date: 2015年7月3日 下午4:40:54 QQ2050542273
 * @email:15162925211@163.com
 */
@SuppressLint("NewApi")
public class MessageChatGroupListAdapter extends BaseAdapter {

    public static final Pattern EMOTION_URL = Pattern.compile("\\[(\\S+?)\\]");
    private Context context;
    private List<ImMessage> userList = new ArrayList<ImMessage>();

    private ArrayList<String> imageList = new ArrayList<String>();
    private HashMap<Integer, Integer> imagePosition = new HashMap<Integer, Integer>();
    public static final int FROM_USER_MSG = 0;//接收文本消息类型
    public static final int TO_USER_MSG = 1;//发送文本消息类型
    public static final int FROM_USER_IMG = 2;//接收图片消息类型
    public static final int TO_USER_IMG = 3;//发送图片消息类型
    public static final int FROM_USER_VOICE = 4;//接收语音消息类型
    public static final int TO_USER_VOICE = 5;//发送语音消息类型
    public static final int FROM_USER_REDPACKET = 6;//接收红包消息类型
    public static final int TO_USER_REDPACKET = 7;//发送红包消息类型
    public static final int SHOW_PROMP_RED = 8;//红包领取的提示小框
    public static final int TO_USER_TRANSFER = 9;//发送转账消息
    public static final int FROM_USER_TRANSFER = 10;//接受转账消息
    public static final int FROM_USER_TRANSFER_OTHER_GET = 11;//转账被接受消息
    public static final int TO_USER_TRANSFER_OTHER_GET = 12;//发送转账被接受消息
    public static final int SHOW_TRANSFER_BACK = 13;//转账退回
    public static final int SHOW_REDPACKET_BACK = 14;//红包退回
    public static final int SHOW_PROMP_JOIN_GROUP = 30;//有人加入群
    public static final int SHOW_PROMP_DELETE_GROUP = 31;//有人退出群
    public static final int SHOW_PROMP_REDPACKET_NOFINISH = 32;//红包被领完
    public static final int VIEW_TYPE_COUNT = 18;
    public static final int Lucky_Red_Envelope = 43;//发送幸运红包
    private int mMinItemWith;// 设置对话框的最大宽度和最小宽度
    private int mMaxItemWith;
    public Handler handler;
    private Animation an;
    private SendErrorListener sendErrorListener;
    private VoiceIsRead voiceIsRead;
    public List<String> unReadPosition = new ArrayList<String>();
    private int voicePlayPosition = -1;
    private LayoutInflater mLayoutInflater;
    private boolean isGif = true;
    public boolean isPicRefresh = true;
    private Handler mHandler;
    private RedPacketOnClick mRedPacketOnClick;
    private FanYiOnClick mFanYiOnClick;
    private HeadUserOnClick mHeadUserOnClick;
    private String selfHeadImg;
    DecimalFormat df = new DecimalFormat("######0.00");
    private List<ImFriendEntivity> mFriendEntivities = new ArrayList<>();

    public void setImFriendEntivity(List<ImFriendEntivity> friendEntivities) {
        mFriendEntivities = friendEntivities;
    }

    public void myNotifyDataSetChanged(List<ImMessage> imMessages) {
        if (imMessages==null){
            notifyDataSetChanged();
        }else {
            userList = imMessages;
            notifyDataSetChanged();
        }
    }

    private String getdoubTwoMoney(String inStr) {
        double money = 0;
        if (!"".equals(inStr)) {
            money = Double.parseDouble(inStr);
        }
        return df.format(money);
    }

    public interface SendErrorListener {
        public void onClick(int position);
    }

    public void setSendErrorListener(SendErrorListener sendErrorListener) {
        this.sendErrorListener = sendErrorListener;
    }

    public interface VoiceIsRead {
        public void voiceOnClick(int position);
    }

    public void setVoiceIsReadListener(VoiceIsRead voiceIsRead) {
        this.voiceIsRead = voiceIsRead;
    }

    public interface RedPacketOnClick {
        void redOnClick(String content, int type);//0-获取红包；1-发送红包；2-获取转账；3-发送转账
    }

    public void setRedPacketListener(RedPacketOnClick redPacketOnClick) {
        this.mRedPacketOnClick = redPacketOnClick;
    }

    public interface HeadUserOnClick {
        void detailsOnClick(int position, int type);//0-好友头像点击；1-我的头像点击(先做上，省的以后加)
    }

    public void setHeadUserListener(HeadUserOnClick mHeadUserOnClick) {
        this.mHeadUserOnClick = mHeadUserOnClick;
    }

    public void setFanYiListener(FanYiOnClick mFanYiOnClick) {
        this.mFanYiOnClick = mFanYiOnClick;
    }

    public interface FanYiOnClick {
        void fanYiOnClick(String content, int type, int isRefresh);//isRefresh=0-不是；1-是并显示；2-是并隐藏
    }

    public MessageChatGroupListAdapter(Context context) {
        EventBus.getDefault().register(this);
        selfHeadImg = ToolsUtils.getUser().getHeadUrl();
        if (selfHeadImg == null) {
            selfHeadImg = "";
        }
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        // 获取系统宽度
        WindowManager wManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wManager.getDefaultDisplay().getMetrics(outMetrics);
        mMaxItemWith = (int) (outMetrics.widthPixels * 0.5f);
        mMinItemWith = (int) (outMetrics.widthPixels * 0.15f);
        handler = new Handler();
    }

    public void setImageList(ArrayList<String> imageList) {
        this.imageList = imageList;
    }

    public void setImagePosition(HashMap<Integer, Integer> imagePosition) {
        this.imagePosition = imagePosition;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onStopEvent(String code)//在ui线程执行
    {
        if (code.equals(Constant.STOP_VOICE_PLAY)) {
            stopPlayVoice();
        }
    }


    @Override
    public int getCount() {
        return userList.size();
    }

    @Override
    public Object getItem(int position) {
        return userList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        ImMessage tbub = userList.get(position);
        int itemViewType = tbub.getType();

        switch (itemViewType) {
            case FROM_USER_MSG: {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_msgfrom_list_item, parent, false);
                FromUserMsgViewHolder holder = new FromUserMsgViewHolder(view);
                fromMsgUserLayout(holder, tbub, position);
                break;
            }
            case FROM_USER_IMG: {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_imagefrom_list_item, parent, false);
                FromUserImageViewHolder holder = new FromUserImageViewHolder(view);
                fromImgUserLayout(holder, tbub, position);

                break;
            }
            case FROM_USER_VOICE: {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_voicefrom_list_item, parent, false);
                FromUserVoiceViewHolder holder = new FromUserVoiceViewHolder(view);
                fromVoiceUserLayout(holder, tbub, position);
                break;
            }
            case TO_USER_MSG: {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_msgto_list_item, parent, false);
                ToUserMsgViewHolder holder = new ToUserMsgViewHolder(view);
                toMsgUserLayout(holder, tbub, position);
                break;
            }
            case TO_USER_IMG: {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_imageto_list_item, parent, false);
                ToUserImgViewHolder holder = new ToUserImgViewHolder(view);
                toImgUserLayout(holder, tbub, position);
                break;
            }
            case TO_USER_VOICE: {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_voiceto_list_item, parent, false);
                ToUserVoiceViewHolder holder = new ToUserVoiceViewHolder(view);
                toVoiceUserLayout(holder, tbub, position);
                break;
            }
            case TO_USER_REDPACKET: {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_redpacketto_list_item, parent, false);
                ToUserRedViewHolder holder = new ToUserRedViewHolder(view);
                toRedPacketUserLayout(holder, tbub, position);
                break;
            }
            case FROM_USER_REDPACKET: {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_redpacketfrom_list_item, parent, false);
                FromUserRedViewHolder holder = new FromUserRedViewHolder(view);
                fromRedPacketUserLayout(holder, tbub, position);
                break;
            }

            case Lucky_Red_Envelope: {// 接收到幸运红包
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_redpacketfrom_list_item, parent, false);
                FromUserRedViewHolder holder = new FromUserRedViewHolder(view);
                fromRedPacketUserLayout(holder, tbub, position);
                break;
            }

            case TO_USER_TRANSFER:// 发送转账
            {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_transferto_list_item, parent, false);
                ToUserTransferViewHolder holder = new ToUserTransferViewHolder(view);
                toTransferUserLayout(holder, tbub, position);
                break;
            }
            case FROM_USER_TRANSFER:// 接受转账
            {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_transferfrom_list_item, parent, false);
                FromUserTransferViewHolder holder = new FromUserTransferViewHolder(view);
                fromTransferUserLayout(holder, tbub, position);
                break;
            }
            case TO_USER_TRANSFER_OTHER_GET:// 发送转账bei接受
            {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_transferto_list_item, parent, false);
                ToUserTransferViewHolder holder = new ToUserTransferViewHolder(view);
                toTransferUserLayout2(holder, tbub, position);
            }
            break;
            case FROM_USER_TRANSFER_OTHER_GET:// 转账bei接受
            {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_transferfrom_list_item, parent, false);
                FromUserTransferViewHolder holder = new FromUserTransferViewHolder(view);
                fromTransferUserLayout2(holder, tbub, position);
                break;
            }
            case SHOW_PROMP_RED:// 用于红包领取提示
            {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_prompt_list_item, parent, false);
                PromptViewHolder holder = new PromptViewHolder(view);
                PromptViewLayout(holder, tbub, position);
                break;
            }
            case SHOW_TRANSFER_BACK:// 转账倍退回
            {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_prompt_list_item, parent, false);
                PromptViewHolder holder = new PromptViewHolder(view);
                TransferViewLayout(holder, tbub, position);
                break;
            }
            case SHOW_REDPACKET_BACK:// 红包倍退回
            {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_prompt_list_item, parent, false);
                PromptViewHolder holder = new PromptViewHolder(view);
                RedPacketViewLayout(holder, tbub, position);
                break;
            }

            case SHOW_PROMP_JOIN_GROUP:// 有人加入群组
            {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_prompt_list_item, parent, false);
                PromptViewHolder holder = new PromptViewHolder(view);
                AddGroupViewLayout(holder, tbub, position);
                break;
            }
            case SHOW_PROMP_DELETE_GROUP:// 有人退出群组
            {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_prompt_list_item, parent, false);
                PromptViewHolder holder = new PromptViewHolder(view);
                DeleteGroupViewLayout(holder, tbub, position);
                break;
            }
            case SHOW_PROMP_REDPACKET_NOFINISH://
            {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_prompt_list_item, parent, false);
                PromptViewHolder holder = new PromptViewHolder(view);
                RedPacketViewLayout(holder, tbub, position);
                break;
            }
        }

        return view;
    }


    class ToUserTransferViewHolder {
        private ImageView headicon;
        private ImageView sendFailImg;
        private ImageView image_hb;
        private TextView chat_time;
        private TextView beizhu;
        private TextView name;
        private TextView txt_money;
        private RelativeLayout relative_transfer;

        public ToUserTransferViewHolder(View view) {
            headicon = (ImageView) view
                    .findViewById(R.id.tb_my_user_icon);
            image_hb = (ImageView) view
                    .findViewById(R.id.image_hb);
            name = (TextView) view
                    .findViewById(R.id.name);
            sendFailImg = (ImageView) view
                    .findViewById(R.id.mysend_fail_img);
            chat_time = (TextView) view.findViewById(R.id.mychat_time);
            beizhu = (TextView) view.findViewById(R.id.txt_beizhu);
            txt_money = (TextView) view.findViewById(R.id.txt_money);
            relative_transfer = (RelativeLayout) view.findViewById(R.id.relative_transfer);

        }
    }

    class FromUserTransferViewHolder {
        private ImageView headicon;
        private ImageView sendFailImg;
        private ImageView image_hb;
        private TextView chat_time;
        private TextView name;
        private TextView beizhu;
        private TextView txt_money;
        private RelativeLayout relative_transfer;

        public FromUserTransferViewHolder(View view) {
            image_hb = (ImageView) view
                    .findViewById(R.id.image_hb);
            headicon = (ImageView) view
                    .findViewById(R.id.tb_other_user_icon);
            chat_time = (TextView) view.findViewById(R.id.chat_time);
            beizhu = (TextView) view.findViewById(R.id.txt_beizhu);
            name = (TextView) view.findViewById(R.id.name);
            txt_money = (TextView) view.findViewById(R.id.txt_money);
            relative_transfer = (RelativeLayout) view.findViewById(R.id.relative_transfer);
        }
    }

    class ToUserRedViewHolder {
        private ImageView headicon;
        private ImageView sendFailImg;
        private TextView name;
        private TextView chat_time;
        private TextView beizhu;
        private RelativeLayout relative_redpacket;

        public ToUserRedViewHolder(View view) {
            headicon = (ImageView) view
                    .findViewById(R.id.tb_my_user_icon);
            name = (TextView) view
                    .findViewById(R.id.name);
            sendFailImg = (ImageView) view
                    .findViewById(R.id.mysend_fail_img);
            chat_time = (TextView) view.findViewById(R.id.mychat_time);
            beizhu = (TextView) view.findViewById(R.id.txt_beizhu);
            relative_redpacket = (RelativeLayout) view.findViewById(R.id.relative_redpacket);

        }
    }

    class FromUserRedViewHolder {
        private ImageView headicon;
        private ImageView sendFailImg;
        private TextView chat_time;
        private TextView beizhu;
        private TextView name;
        private RelativeLayout relative_redpacket;


        public FromUserRedViewHolder(View view) {
            headicon = (ImageView) view
                    .findViewById(R.id.tb_other_user_icon);
            chat_time = (TextView) view.findViewById(R.id.chat_time);
            beizhu = (TextView) view.findViewById(R.id.txt_beizhu);
            name = (TextView) view.findViewById(R.id.name);
            relative_redpacket = (RelativeLayout) view.findViewById(R.id.relative_redpacket);
        }
    }

    class PromptViewHolder {
        private TextView txt_red_name;
        private ImageView image_hb;

        public PromptViewHolder(View view) {
            txt_red_name = (TextView) view.findViewById(R.id.txt_red_name);
            image_hb = (ImageView) view.findViewById(R.id.image_hb);
        }
    }

    class FromUserMsgViewHolder {
        ImageView headicon;
        TextView chat_time;
        TextView name;
       // TextView button_fanyi;
        GifTextView content;
        GifTextView content_fy;

        public FromUserMsgViewHolder(View view) {
            headicon = (ImageView) view
                    .findViewById(R.id.tb_other_user_icon);
            name = (TextView) view
                    .findViewById(R.id.name);
            chat_time = (TextView) view.findViewById(R.id.chat_time);
           // button_fanyi = (TextView) view.findViewById(R.id.button_fanyi);
            content = (GifTextView) view.findViewById(R.id.content);
            content_fy = (GifTextView) view.findViewById(R.id.content_fy);
        }
    }

    class FromUserImageViewHolder {
        private ImageView headicon;
        private TextView chat_time;
        private TextView name;
        private BubbleImageView image_Msg;

        public FromUserImageViewHolder(View view) {
            headicon = (ImageView) view
                    .findViewById(R.id.tb_other_user_icon);
            chat_time = (TextView) view.findViewById(R.id.chat_time);
            name = (TextView) view.findViewById(R.id.name);
            image_Msg = (BubbleImageView) view
                    .findViewById(R.id.image_message);
        }
    }

    class FromUserVoiceViewHolder {
        private ImageView headicon;
        private TextView chat_time;
        private LinearLayout voice_group;
        private TextView voice_time;

        private FrameLayout voice_image;
        private View receiver_voice_unread;
        private View voice_anim;

        private TextView name;
        public FromUserVoiceViewHolder(View view) {
            headicon = (ImageView) view
                    .findViewById(R.id.tb_other_user_icon);
            name = (TextView) view.findViewById(R.id.name);
            chat_time = (TextView) view.findViewById(R.id.chat_time);
            voice_group = (LinearLayout) view
                    .findViewById(R.id.voice_group);
            voice_time = (TextView) view
                    .findViewById(R.id.voice_time);
            receiver_voice_unread = (View) view
                    .findViewById(R.id.receiver_voice_unread);
            voice_image = (FrameLayout) view
                    .findViewById(R.id.voice_receiver_image);
            voice_anim = (View) view
                    .findViewById(R.id.id_receiver_recorder_anim);
        }
    }

    class ToUserMsgViewHolder {
        private ImageView headicon;
        private TextView chat_time;
        private TextView name;
        private GifTextView content;
        private ImageView sendFailImg;

        public ToUserMsgViewHolder(View view) {
            headicon = (ImageView) view
                    .findViewById(R.id.tb_my_user_icon);
            chat_time = (TextView) view
                    .findViewById(R.id.mychat_time);
            name = (TextView) view
                    .findViewById(R.id.name);
            content = (GifTextView) view
                    .findViewById(R.id.mycontent);
            sendFailImg = (ImageView) view
                    .findViewById(R.id.mysend_fail_img);
        }
    }

    class ToUserImgViewHolder {
        private ImageView headicon;
        private TextView chat_time;
        private TextView name;
        private LinearLayout image_group;
        private BubbleImageView image_Msg;
        private ImageView sendFailImg;

        public ToUserImgViewHolder(View view) {

            headicon = (ImageView) view
                    .findViewById(R.id.tb_my_user_icon);
            chat_time = (TextView) view
                    .findViewById(R.id.mychat_time);
            name = (TextView) view
                    .findViewById(R.id.name);
            sendFailImg = (ImageView) view
                    .findViewById(R.id.mysend_fail_img);
            image_group = (LinearLayout) view
                    .findViewById(R.id.image_group);
            image_Msg = (BubbleImageView) view
                    .findViewById(R.id.image_message);
        }
    }

    class ToUserVoiceViewHolder {
        private ImageView headicon;
        private TextView chat_time;
        private LinearLayout voice_group;
        private TextView name;
        private TextView voice_time;
        private FrameLayout voice_image;
        private View receiver_voice_unread;
        private View voice_anim;
        private ImageView sendFailImg;

        public ToUserVoiceViewHolder(View view) {

            headicon = (ImageView) view
                    .findViewById(R.id.tb_my_user_icon);
            name = (TextView) view
                    .findViewById(R.id.name);
            chat_time = (TextView) view
                    .findViewById(R.id.mychat_time);
            voice_group = (LinearLayout) view
                    .findViewById(R.id.voice_group);
            voice_time = (TextView) view
                    .findViewById(R.id.voice_time);
            voice_image = (FrameLayout) view
                    .findViewById(R.id.voice_image);
            voice_anim = (View) view
                    .findViewById(R.id.id_recorder_anim);
            sendFailImg = (ImageView) view
                    .findViewById(R.id.mysend_fail_img);
        }
    }


    private void AddGroupViewLayout(final PromptViewHolder holder, final ImMessage tbub, final int position) {
        ImFriendEntivity mmm = new Gson().fromJson(tbub.getContent(), ImFriendEntivity.class);
        String name = mmm.getName();
        if (null == name || "".equals(name)) {
            name = context.getResources().getString(R.string.have_people);
        }
//        String name = ToolsUtils.getGroupUserName(mFriendEntivities, tbub.getFromid());
        holder.txt_red_name.setText(name + context.getString(R.string.join_group));
    }

    private void DeleteGroupViewLayout(final PromptViewHolder holder, final ImMessage tbub, final int position) {
        ImFriendEntivity mmm = new Gson().fromJson(tbub.getContent(), ImFriendEntivity.class);
        String name = mmm.getName();
        if (null == name || "".equals(name)) {
            name = context.getResources().getString(R.string.have_people);
        }
        holder.txt_red_name.setText(name + context.getString(R.string.quit_group));
    }

    private void fromMsgUserLayout(final FromUserMsgViewHolder holder, final ImMessage tbub, final int position) {
        GlideUtils.loadHeadCircularImage(context, tbub.getImageIconUrl(), holder.headicon);
        holder.name.setText(tbub.getFromname());
        holder.name.setVisibility(View.VISIBLE);
        holder.headicon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mHeadUserOnClick.detailsOnClick(position, 0);
            }
        });
        /* time */
        if (position != 0) {
            String showTime = getTime(tbub.getSendTime() + "", userList.get(position - 1)
                    .getSendTime() + "");
            if (showTime != null) {
                holder.chat_time.setVisibility(View.VISIBLE);
                holder.chat_time.setText(showTime);
            } else {
                holder.chat_time.setVisibility(View.GONE);
            }
        } else {
            String showTime = getTime(tbub.getSendTime() + "", null);
            holder.chat_time.setVisibility(View.VISIBLE);
            holder.chat_time.setText(showTime);
        }
        holder.content.setVisibility(View.VISIBLE);
        holder.content.setSpanText(handler, tbub.getContent(), false);
//        holder.content.insertGif(convertNormalStringToSpannableString(tbub.getContent()));


        /*if (null == tbub.getIsShowFY() || !"1".equals(tbub.getIsShowFY())) {//不显示翻译
            holder.content_fy.setVisibility(View.GONE);
            holder.button_fanyi.setText(context.getResources().getString(R.string.chat_button_fanyi));
        } else {//显示翻译
            holder.content_fy.setVisibility(View.VISIBLE);
            FanYiBean mFanYi = new Gson().fromJson(tbub.getContent_fy(), FanYiBean.class);
            holder.content_fy.setText(mFanYi.getTrans_result().get(0).getDst());
            holder.button_fanyi.setText(context.getResources().getString(R.string.chat_button_yinc));

        }

        holder.button_fanyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null == tbub.getIsShowFY() || !"1".equals(tbub.getIsShowFY())) {//翻译
                    if (null == tbub.getContent_fy() || "".equals(tbub.getContent_fy())) {
                        mFanYiOnClick.fanYiOnClick(tbub.getContent(), position, 0);
                    } else {
                        mFanYiOnClick.fanYiOnClick("", position, 1);
                    }
                } else {//隐藏
                    mFanYiOnClick.fanYiOnClick("", position, 2);
                }

            }
        });*/

    }

    private void fromImgUserLayout(final FromUserImageViewHolder holder, final ImMessage tbub, final int position) {

        GlideUtils.loadHeadCircularImage(context, tbub.getImageIconUrl(), holder.headicon);
        holder.name.setText(tbub.getFromname());
        holder.name.setVisibility(View.VISIBLE);
        holder.headicon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mHeadUserOnClick.detailsOnClick(position, 0);
            }
        });

        /* time */
        if (position != 0) {
            String showTime = getTime(tbub.getSendTime() + "", userList.get(position - 1)
                    .getSendTime() + "");
            if (showTime != null) {
                holder.chat_time.setVisibility(View.VISIBLE);
                holder.chat_time.setText(showTime);
            } else {
                holder.chat_time.setVisibility(View.GONE);
            }
        } else {
            String showTime = getTime(tbub.getSendTime() + "", null);
            holder.chat_time.setVisibility(View.VISIBLE);
            holder.chat_time.setText(showTime);
        }

        int res;
        res = R.drawable.chatfrom_bg_focused;

        int width = DimenUtils.getScreenWidth() / Constant.CHAT_MESSAGE_IMG_SIZE_SCALE;
        Glide.with(context).load(tbub.getContent()).transform(new CustomShapeTransformation(context, res)).diskCacheStrategy(DiskCacheStrategy.ALL).override(width, width).dontAnimate().into(holder.image_Msg);
        holder.image_Msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopPlayVoice();
                Intent intent = new Intent(context, ImageViewActivity.class);
                intent.putStringArrayListExtra("images", imageList);
                intent.putExtra("clickedIndex", imagePosition.get(position));
                context.startActivity(intent);
            }

        });
//        }

    }

    private void fromVoiceUserLayout(final FromUserVoiceViewHolder holder, final ImMessage tbub, final int position) {
        GlideUtils.loadHeadCircularImage(context, tbub.getImageIconUrl(), holder.headicon);
        holder.name.setText(tbub.getFromname());
        holder.name.setVisibility(View.VISIBLE);
        holder.headicon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mHeadUserOnClick.detailsOnClick(position, 0);
            }
        });
        /* time */
        if (position != 0) {
            String showTime = getTime(tbub.getSendTime() + "", userList.get(position - 1)
                    .getSendTime() + "");
            if (showTime != null) {
                holder.chat_time.setVisibility(View.VISIBLE);
                holder.chat_time.setText(showTime);
            } else {
                holder.chat_time.setVisibility(View.GONE);
            }
        } else {
            String showTime = getTime(tbub.getSendTime() + "", null);
            holder.chat_time.setVisibility(View.VISIBLE);
            holder.chat_time.setText(showTime);
        }

        holder.voice_group.setVisibility(View.VISIBLE);
        if (holder.receiver_voice_unread != null)
            holder.receiver_voice_unread.setVisibility(View.GONE);
        if (holder.receiver_voice_unread != null && unReadPosition != null) {
            for (String unRead : unReadPosition) {
                if (unRead.equals(position + "")) {
                    holder.receiver_voice_unread
                            .setVisibility(View.VISIBLE);
                    break;
                }
            }
        }
        AnimationDrawable drawable;
        holder.voice_anim.setId(position);
        if (position == voicePlayPosition) {
            holder.voice_anim
                    .setBackgroundResource(R.drawable.receiver_voice_node_playing003);
            holder.voice_anim
                    .setBackgroundResource(R.drawable.voice_play_receiver);
            drawable = (AnimationDrawable) holder.voice_anim
                    .getBackground();
            drawable.start();
        } else {
            holder.voice_anim
                    .setBackgroundResource(R.drawable.receiver_voice_node_playing003);
        }
        holder.voice_group.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (holder.receiver_voice_unread != null)
                    holder.receiver_voice_unread.setVisibility(View.GONE);
                holder.voice_anim
                        .setBackgroundResource(R.drawable.receiver_voice_node_playing003);
                if (voicePlayPosition == -1) {
                    stopPlayVoice();
                    voicePlayPosition = holder.voice_anim.getId();
                    String voicePath = tbub.getUserVoiceUrl() == null ? "" : tbub
                            .getUserVoiceUrl();

                    if (!voicePath.equals("")) {
                        AnimationDrawable drawable;
                        holder.voice_anim
                                .setBackgroundResource(R.drawable.voice_play_receiver);
                        drawable = (AnimationDrawable) holder.voice_anim
                                .getBackground();
                        drawable.start();

                        if (voiceIsRead != null) {
                            voiceIsRead.voiceOnClick(position);
                        }

                        MediaManager.playSound(context,voicePath,
                                new MediaPlayer.OnCompletionListener() {

                                    @Override
                                    public void onCompletion(MediaPlayer mp) {
                                        voicePlayPosition = -1;
                                        holder.voice_anim
                                                .setBackgroundResource(R.drawable.receiver_voice_node_playing003);
                                    }
                                },AudioManager.STREAM_MUSIC);

                    }
                } else {
                    stopPlayVoice();
                }

            }

        });
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case Constant.ADAPTER_VOICE_FROM_DOWN_FINISH:
                        if (msg.arg1 == position) {
                            AnimationDrawable drawable;
                            holder.voice_anim
                                    .setBackgroundResource(R.drawable.voice_play_receiver);
                            drawable = (AnimationDrawable) holder.voice_anim
                                    .getBackground();
                            drawable.start();

                            if (voiceIsRead != null) {
                                voiceIsRead.voiceOnClick(position);
                            }
                            MediaManager.playSound(context,(String) msg.obj,
                                    new MediaPlayer.OnCompletionListener() {

                                        @Override
                                        public void onCompletion(MediaPlayer mp) {
                                            voicePlayPosition = -1;
                                            holder.voice_anim
                                                    .setBackgroundResource(R.drawable.receiver_voice_node_playing003);
                                        }
                                    }, AudioManager.STREAM_MUSIC);
                        }
                        break;
                }
            }
        };

        float voiceTime = tbub.getUserVoiceTime();
        Log.i("-----", "onCompletion:=002voiceTime=" + voiceTime);
        BigDecimal b = new BigDecimal(voiceTime);
        float f1 = b.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
        holder.voice_time.setText(f1 + "\"");
        ViewGroup.LayoutParams lParams = holder.voice_image
                .getLayoutParams();
        lParams.width = (int) (mMinItemWith + mMaxItemWith / 90f
                * tbub.getUserVoiceTime());
        holder.voice_image.setLayoutParams(lParams);

    }

    private void toMsgUserLayout(final ToUserMsgViewHolder holder, final ImMessage tbub, final int position) {
        GlideUtils.loadHeadCircularImage(context, selfHeadImg, holder.headicon);
        holder.name.setText(ToolsUtils.getNick(ToolsUtils.getUser()));
        switch (tbub.getSendState()) {
            case Constant.SENDING:
                an = AnimationUtils.loadAnimation(context,
                        R.anim.update_loading_progressbar_anim);
                LinearInterpolator lin = new LinearInterpolator();
                an.setInterpolator(lin);
                an.setRepeatCount(-1);
                holder.sendFailImg
                        .setBackgroundResource(R.drawable.loading);
                holder.sendFailImg.startAnimation(an);
                an.startNow();
                holder.sendFailImg.setVisibility(View.VISIBLE);
                break;

            case Constant.COMPLETED:
                holder.sendFailImg.clearAnimation();
                holder.sendFailImg.setVisibility(View.GONE);
                break;

            case Constant.SENDERROR:
                holder.sendFailImg.clearAnimation();
                holder.sendFailImg
                        .setBackgroundResource(R.drawable.msg_state_fail_resend_pressed);
                holder.sendFailImg.setVisibility(View.VISIBLE);
                holder.sendFailImg.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        if (sendErrorListener != null) {
                            sendErrorListener.onClick(position);
                        }
                    }

                });
                break;
            default:
                break;
        }
        /* time */
        if (position != 0) {
            String showTime = getTime(tbub.getSendTime() + "", userList.get(position - 1)
                    .getSendTime() + "");
            if (showTime != null) {
                holder.chat_time.setVisibility(View.VISIBLE);
                holder.chat_time.setText(showTime);
            } else {
                holder.chat_time.setVisibility(View.GONE);
            }
        } else {
            String showTime = getTime(tbub.getSendTime() + "", null);
            holder.chat_time.setVisibility(View.VISIBLE);
            holder.chat_time.setText(showTime);
        }

        holder.content.setVisibility(View.VISIBLE);

//        holder.content.setSpanText(handler, tbub.getContent(), false);
//        holder.content.insertGif(convertNormalStringToSpannableString(tbub.getContent()));
        holder.content.setSpanText(handler, tbub.getContent(), false);

    }

    private void toImgUserLayout(final ToUserImgViewHolder holder, final ImMessage tbub, final int position) {
        //本地图片加载本地资源
        GlideUtils.loadHeadCircularImage(context, selfHeadImg, holder.headicon);
        holder.name.setText(ToolsUtils.getNick(ToolsUtils.getUser()));
        switch (tbub.getSendState()) {
            case Constant.SENDING:
                an = AnimationUtils.loadAnimation(context,
                        R.anim.update_loading_progressbar_anim);
                LinearInterpolator lin = new LinearInterpolator();
                an.setInterpolator(lin);
                an.setRepeatCount(-1);
                holder.sendFailImg
                        .setBackgroundResource(R.drawable.loading);
                holder.sendFailImg.startAnimation(an);
                an.startNow();
                holder.sendFailImg.setVisibility(View.VISIBLE);
                break;

            case Constant.COMPLETED:
                holder.sendFailImg.clearAnimation();
                holder.sendFailImg.setVisibility(View.GONE);
                break;

            case Constant.SENDERROR:
                holder.sendFailImg.clearAnimation();
                holder.sendFailImg
                        .setBackgroundResource(R.drawable.msg_state_fail_resend_pressed);
                holder.sendFailImg.setVisibility(View.VISIBLE);
                holder.sendFailImg.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        if (sendErrorListener != null) {
                            sendErrorListener.onClick(position);
                        }
                    }

                });
                break;
            default:
                break;
        }

        /* time */
        if (position != 0) {
            String showTime = getTime(tbub.getSendTime() + "", userList.get(position - 1)
                    .getSendTime() + "");
            if (showTime != null) {
                holder.chat_time.setVisibility(View.VISIBLE);
                holder.chat_time.setText(showTime);
            } else {
                holder.chat_time.setVisibility(View.GONE);
            }
        } else {
            String showTime = getTime(tbub.getSendTime() + "", null);
            holder.chat_time.setVisibility(View.VISIBLE);
            holder.chat_time.setText(showTime);
        }

        holder.image_group.setVisibility(View.VISIBLE);

        String imageUrlSrc = "";
        if (tbub.getFilePath() != null && !tbub.getFilePath().equals("")) {
            imageUrlSrc = tbub.getFilePath();
        } else if (tbub.getFileUrl() != null) {
            imageUrlSrc = tbub.getFileUrl();
        }
        int res;
        res = R.drawable.chatto_bg_focused;
        int width = DimenUtils.getScreenWidth() / Constant.CHAT_MESSAGE_IMG_SIZE_SCALE;
        Glide.with(context).load(imageUrlSrc).diskCacheStrategy(DiskCacheStrategy.ALL).transform(new CustomShapeTransformation(context, res)).override(width, width).dontAnimate().into(holder.image_Msg);
        holder.image_Msg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                stopPlayVoice();
                Intent intent = new Intent(context, ImageViewActivity.class);
                intent.putStringArrayListExtra("images", imageList);
                intent.putExtra("clickedIndex", imagePosition.get(position));
                context.startActivity(intent);
            }

        });

//        }
    }

    private void toVoiceUserLayout(final ToUserVoiceViewHolder holder, final ImMessage tbub, final int position) {
        GlideUtils.loadHeadCircularImage(context, selfHeadImg, holder.headicon);
        holder.name.setText(ToolsUtils.getNick(ToolsUtils.getUser()));
        switch (tbub.getSendState()) {
            case Constant.SENDING:
                an = AnimationUtils.loadAnimation(context,
                        R.anim.update_loading_progressbar_anim);
                LinearInterpolator lin = new LinearInterpolator();
                an.setInterpolator(lin);
                an.setRepeatCount(-1);
                holder.sendFailImg
                        .setBackgroundResource(R.drawable.xsearch_loading);
                holder.sendFailImg.startAnimation(an);
                an.startNow();
                holder.sendFailImg.setVisibility(View.VISIBLE);
                break;

            case Constant.COMPLETED:
                holder.sendFailImg.clearAnimation();
                holder.sendFailImg.setVisibility(View.GONE);
                break;

            case Constant.SENDERROR:
                holder.sendFailImg.clearAnimation();
                holder.sendFailImg
                        .setBackgroundResource(R.drawable.msg_state_fail_resend_pressed);
                holder.sendFailImg.setVisibility(View.VISIBLE);
                holder.sendFailImg.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        // TODO Auto-generated method stub
                        if (sendErrorListener != null) {
                            sendErrorListener.onClick(position);
                        }
                    }

                });
                break;
            default:
                break;
        }

        /* time */
        if (position != 0) {
            String showTime = getTime(tbub.getSendTime() + "", userList.get(position - 1)
                    .getSendTime() + "");
            if (showTime != null) {
                holder.chat_time.setVisibility(View.VISIBLE);
                holder.chat_time.setText(showTime);
            } else {
                holder.chat_time.setVisibility(View.GONE);
            }
        } else {
            String showTime = getTime(tbub.getSendTime()
                    + "", null);
            holder.chat_time.setVisibility(View.VISIBLE);
            holder.chat_time.setText(showTime);
        }
        holder.voice_group.setVisibility(View.VISIBLE);
        if (holder.receiver_voice_unread != null)
            holder.receiver_voice_unread.setVisibility(View.GONE);
        if (holder.receiver_voice_unread != null && unReadPosition != null) {
            for (String unRead : unReadPosition) {
                if (unRead.equals(position + "")) {
                    holder.receiver_voice_unread
                            .setVisibility(View.VISIBLE);
                    break;
                }
            }
        }
        AnimationDrawable drawable;
        holder.voice_anim.setId(position);
        if (position == voicePlayPosition) {
            holder.voice_anim.setBackgroundResource(R.drawable.adj);
            holder.voice_anim
                    .setBackgroundResource(R.drawable.voice_play_send);
            drawable = (AnimationDrawable) holder.voice_anim
                    .getBackground();
            drawable.start();
        } else {
            holder.voice_anim.setBackgroundResource(R.drawable.adj);
        }
        holder.voice_group.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (holder.receiver_voice_unread != null)
                    holder.receiver_voice_unread.setVisibility(View.GONE);
                holder.voice_anim.setBackgroundResource(R.drawable.adj);

                if (voicePlayPosition == -1) {
                    stopPlayVoice();
                    voicePlayPosition = holder.voice_anim.getId();
                    AnimationDrawable drawable;
                    holder.voice_anim
                            .setBackgroundResource(R.drawable.voice_play_send);
                    drawable = (AnimationDrawable) holder.voice_anim
                            .getBackground();
                    drawable.start();
                    String voicePath = tbub.getFileUrl() == null ? ""
                            : tbub.getFileUrl();
                    if (voiceIsRead != null) {
                        voiceIsRead.voiceOnClick(position);
                    }

                    MediaManager.playSound(context,voicePath,
                            new MediaPlayer.OnCompletionListener() {

                                @Override
                                public void onCompletion(MediaPlayer mp) {
                                    Log.i("-----", "onCompletion: 停止播放老=" + voicePlayPosition);
                                    voicePlayPosition = -1;
                                    holder.voice_anim
                                            .setBackgroundResource(R.drawable.adj);
                                }
                            },AudioManager.STREAM_MUSIC);
                } else {
                    stopPlayVoice();
                }

            }

        });
        float voiceTime = tbub.getUserVoiceTime();
        Log.i("-----", "onCompletion:=001voiceTime=" + voiceTime);
        BigDecimal b = new BigDecimal(voiceTime);
        float f1 = b.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
        holder.voice_time.setText(f1 + "\"");
        ViewGroup.LayoutParams lParams = holder.voice_image
                .getLayoutParams();
        lParams.width = (int) (mMinItemWith + mMaxItemWith / 90f
                * tbub.getUserVoiceTime());
        holder.voice_image.setLayoutParams(lParams);
    }

    private void toRedPacketUserLayout(final ToUserRedViewHolder holder, final ImMessage tbub, final int position) {
        GlideUtils.loadHeadCircularImage(context, selfHeadImg, holder.headicon);
        holder.name.setText(ToolsUtils.getNick(ToolsUtils.getUser()));
        switch (1) {
            case Constant.SENDING:
                break;
            case Constant.COMPLETED:
                holder.sendFailImg.clearAnimation();
                holder.sendFailImg.setVisibility(View.GONE);
                break;

            case Constant.SENDERROR:
                holder.sendFailImg.clearAnimation();
                holder.sendFailImg
                        .setBackgroundResource(R.drawable.msg_state_fail_resend_pressed);
                holder.sendFailImg.setVisibility(View.VISIBLE);
                holder.sendFailImg.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        // TODO Auto-generated method stub
                        if (sendErrorListener != null) {
                            sendErrorListener.onClick(position);
                        }
                    }

                });
                break;
            default:
                break;
        }

        /* time */
        if (position != 0) {
            String showTime = getTime(tbub.getSendTime() + "", userList.get(position - 1)
                    .getSendTime() + "");
            if (showTime != null) {
                holder.chat_time.setVisibility(View.VISIBLE);
                holder.chat_time.setText(showTime);
            } else {
                holder.chat_time.setVisibility(View.GONE);
            }
        } else {
            String showTime = getTime(tbub.getSendTime()
                    + "", null);
            holder.chat_time.setVisibility(View.VISIBLE);
            holder.chat_time.setText(showTime);
        }

        holder.relative_redpacket.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRedPacketOnClick.redOnClick(tbub.getContent(), 1);
            }
        });
        RedPacketEntivity mRedPacketEntivity = new Gson().fromJson(tbub.getContent(), RedPacketEntivity.class);
        holder.beizhu.setText("".equals(mRedPacketEntivity.getMsg()) ? context.getString(R.string.ordinary_red_packet_messagemr) : mRedPacketEntivity.getMsg());

    }

    private void fromRedPacketUserLayout(final FromUserRedViewHolder holder, final ImMessage tbub, final int position) {
        GlideUtils.loadHeadCircularImage(context, tbub.getImageIconUrl(), holder.headicon);
        holder.name.setText(tbub.getFromname());
        holder.name.setVisibility(View.VISIBLE);
        holder.headicon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mHeadUserOnClick.detailsOnClick(position, 0);
            }
        });

        /* time */
        if (position != 0) {
            String showTime = getTime(tbub.getSendTime() + "", userList.get(position - 1).getSendTime() + "");
            if (showTime != null) {
                holder.chat_time.setVisibility(View.VISIBLE);
                holder.chat_time.setText(showTime);
            } else {
                holder.chat_time.setVisibility(View.GONE);
            }
        } else {
            String showTime = getTime(tbub.getSendTime()
                    + "", null);
            holder.chat_time.setVisibility(View.VISIBLE);
            holder.chat_time.setText(showTime);
        }

//        mRedPacketOnClick
        holder.relative_redpacket.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRedPacketOnClick.redOnClick(tbub.getContent(), 0);
            }
        });

        RedPacketEntivity mRedPacketEntivity = new Gson().fromJson(tbub.getContent(), RedPacketEntivity.class);

        holder.beizhu.setText("".equals(mRedPacketEntivity.getMsg()) ? context.getString(R.string.ordinary_red_packet_messagemr) : mRedPacketEntivity.getMsg());

    }

    private void toTransferUserLayout(final ToUserTransferViewHolder holder, final ImMessage tbub, final int position) {
        GlideUtils.loadHeadCircularImage(context, selfHeadImg, holder.headicon);
        holder.name.setText(ToolsUtils.getNick(ToolsUtils.getUser()));
        switch (1) {
            case Constant.SENDING:
                break;
            case Constant.COMPLETED:
                holder.sendFailImg.clearAnimation();
                holder.sendFailImg.setVisibility(View.GONE);
                break;

            case Constant.SENDERROR:
                holder.sendFailImg.clearAnimation();
                holder.sendFailImg
                        .setBackgroundResource(R.drawable.msg_state_fail_resend_pressed);
                holder.sendFailImg.setVisibility(View.VISIBLE);
                holder.sendFailImg.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        // TODO Auto-generated method stub
                        if (sendErrorListener != null) {
                            sendErrorListener.onClick(position);
                        }
                    }

                });
                break;
            default:
                break;
        }

        /* time */
        if (position != 0) {
            String showTime = getTime(tbub.getSendTime() + "", userList.get(position - 1)
                    .getSendTime() + "");
            if (showTime != null) {
                holder.chat_time.setVisibility(View.VISIBLE);
                holder.chat_time.setText(showTime);
            } else {
                holder.chat_time.setVisibility(View.GONE);
            }
        } else {
            String showTime = getTime(tbub.getSendTime()
                    + "", null);
            holder.chat_time.setVisibility(View.VISIBLE);
            holder.chat_time.setText(showTime);
        }
        TransferEntivity mTransferEntivity = ToolsUtils.getTransfer(tbub.getContent());
        if (null != mTransferEntivity) {
            holder.beizhu.setText(mTransferEntivity.getMsg());
            holder.txt_money.setText(getdoubTwoMoney(mTransferEntivity.getAmount()));
        }

        holder.relative_transfer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRedPacketOnClick.redOnClick(tbub.getContent(), 3);
            }
        });
    }

    private void toTransferUserLayout2(final ToUserTransferViewHolder holder, final ImMessage tbub, final int position) {
        GlideUtils.loadHeadCircularImage(context, selfHeadImg, holder.headicon);

        switch (1) {
            case Constant.SENDING:
                break;
            case Constant.COMPLETED:
                holder.sendFailImg.clearAnimation();
                holder.sendFailImg.setVisibility(View.GONE);
                break;

            case Constant.SENDERROR:
                holder.sendFailImg.clearAnimation();
                holder.sendFailImg
                        .setBackgroundResource(R.drawable.msg_state_fail_resend_pressed);
                holder.sendFailImg.setVisibility(View.VISIBLE);
                holder.sendFailImg.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        if (sendErrorListener != null) {
                            sendErrorListener.onClick(position);
                        }
                    }
                });
                break;
            default:
                break;
        }

        /* time */
        if (position != 0) {
            String showTime = getTime(tbub.getSendTime() + "", userList.get(position - 1)
                    .getSendTime() + "");
            if (showTime != null) {
                holder.chat_time.setVisibility(View.VISIBLE);
                holder.chat_time.setText(showTime);
            } else {
                holder.chat_time.setVisibility(View.GONE);
            }
        } else {
            String showTime = getTime(tbub.getSendTime()
                    + "", null);
            holder.chat_time.setVisibility(View.VISIBLE);
            holder.chat_time.setText(showTime);
        }
        TransferEntivity mTransferEntivity = ToolsUtils.getTransfer(tbub.getContent());
        if (null != mTransferEntivity) {
            holder.beizhu.setText(context.getResources().getString(R.string.get_money_yes));
            holder.txt_money.setText(getdoubTwoMoney(mTransferEntivity.getAmount()));
        }
        holder.image_hb.setBackgroundResource(R.drawable.shouqian);
        holder.relative_transfer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                mRedPacketOnClick.redOnClick(tbub.getContent(), 3);
                //这个情况是已收钱的点击，对方的收钱应该是我发的转账
                mRedPacketOnClick.redOnClick(tbub.getContent(), 2);
            }
        });
    }

    private void fromTransferUserLayout(final FromUserTransferViewHolder holder, final ImMessage tbub, final int position) {
        GlideUtils.loadHeadCircularImage(context, tbub.getImageIconUrl(), holder.headicon);
        holder.name.setText(tbub.getFromname());
        holder.name.setVisibility(View.VISIBLE);
        holder.headicon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mHeadUserOnClick.detailsOnClick(position, 0);
            }
        });
        /* time */
        if (position != 0) {
            String showTime = getTime(tbub.getSendTime() + "", userList.get(position - 1).getSendTime() + "");
            if (showTime != null) {
                holder.chat_time.setVisibility(View.VISIBLE);
                holder.chat_time.setText(showTime);
            } else {
                holder.chat_time.setVisibility(View.GONE);
            }
        } else {
            String showTime = getTime(tbub.getSendTime()
                    + "", null);
            holder.chat_time.setVisibility(View.VISIBLE);
            holder.chat_time.setText(showTime);
        }
        TransferEntivity mTransferEntivity = ToolsUtils.getTransfer(tbub.getContent());
        if (null != mTransferEntivity) {
            holder.beizhu.setText(mTransferEntivity.getMsg());
            holder.txt_money.setText(getdoubTwoMoney(mTransferEntivity.getAmount()));
        }

        holder.relative_transfer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRedPacketOnClick.redOnClick(tbub.getContent(), 2);
            }
        });


    }

    /**
     * 转账被领取
     */
    private void fromTransferUserLayout2(final FromUserTransferViewHolder holder, final ImMessage tbub, final int position) {
        GlideUtils.loadHeadCircularImage(context, tbub.getImageIconUrl(), holder.headicon);

        holder.headicon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mHeadUserOnClick.detailsOnClick(position, 0);
            }
        });

        /* time */
        if (position != 0) {
            String showTime = getTime(tbub.getSendTime() + "", userList.get(position - 1).getSendTime() + "");
            if (showTime != null) {
                holder.chat_time.setVisibility(View.VISIBLE);
                holder.chat_time.setText(showTime);
            } else {
                holder.chat_time.setVisibility(View.GONE);
            }
        } else {
            String showTime = getTime(tbub.getSendTime()
                    + "", null);
            holder.chat_time.setVisibility(View.VISIBLE);
            holder.chat_time.setText(showTime);
        }
        TransferEntivity mTransferEntivity = ToolsUtils.getTransfer(tbub.getContent());
        if (null != mTransferEntivity) {
            holder.beizhu.setText(R.string.yet_receive_money);
            holder.txt_money.setText(getdoubTwoMoney(mTransferEntivity.getAmount()));
        }
        holder.image_hb.setBackgroundResource(R.drawable.shouqian);
        holder.relative_transfer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                mRedPacketOnClick.redOnClick(tbub.getContent(), 2);
                //这个情况是已收钱的点击，对方的收钱应该是我发的转账
                mRedPacketOnClick.redOnClick(tbub.getContent(), 3);
            }
        });
    }

    private void PromptViewLayout(final PromptViewHolder holder, final ImMessage tbub, final int position) {
        holder.txt_red_name.setText(R.string.yout_red_package_already_received);
        holder.image_hb.setVisibility(View.VISIBLE);
    }

    private void TransferViewLayout(final PromptViewHolder holder, final ImMessage tbub, final int position) {
        holder.txt_red_name.setText(R.string.transfer_overtime_money_already_back);
    }

    private void RedPacketViewLayout(final PromptViewHolder holder, final ImMessage tbub, final int position) {
        holder.txt_red_name.setText(R.string.red_package_overtime_balance_already_back);
    }

    @SuppressLint("SimpleDateFormat")
    public String getTime(String time, String before) {
        if ("".equals(time) || "0".equals(time) || time == null)
            time = returnTime();
        String show_time = null;
        if (before != null && !before.equals("0")) {
            try {
                if (time.length() == 13) {
                    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    time = sDateFormat.format(new Date(Long.parseLong(time)));
                }
                if (before.length() == 13) {
                    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    before = sDateFormat.format(new Date(Long.parseLong(before)));
                }
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date now = df.parse(time);
                Date date = df.parse(before);
                long l = now.getTime() - date.getTime();
                long day = l / (24 * 60 * 60 * 1000);
                long hour = (l / (60 * 60 * 1000) - day * 24);
                long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
                if (min >= 1) {
                    show_time = time.substring(11);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            if (time.length() > 11) {
                SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                time = sDateFormat.format(new Date(Long.parseLong(time)));
                show_time = time.substring(11);
            }
        }
        String getDay = getDay(time);
        if (show_time != null && getDay != null)
            show_time = getDay + " " + show_time;
        return show_time;
    }

    @SuppressLint("SimpleDateFormat")
    public static String returnTime() {
        SimpleDateFormat sDateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        String date = sDateFormat.format(new Date());
        return date;
    }

    @SuppressLint("SimpleDateFormat")
    public String getDay(String time) {
        String showDay = null;
        String nowTime = returnTime();

        if (null == time || time.equals("null") || "".equals(time))
            time = returnTime();
        try {
            if (time.length() == 13) {
                SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                time = sDateFormat.format(new Date(Long.parseLong(time)));
            }
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date now = df.parse(nowTime);
            Date date = df.parse(time);
            long l = now.getTime() - date.getTime();
            long day = l / (24 * 60 * 60 * 1000);
            if (day >= 365) {
                showDay = time.substring(0, 10);
            } else if (day >= 1 && day < 365) {
                showDay = time.substring(5, 10);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return showDay;
    }

    public static Bitmap getLoacalBitmap(String url) {
        try {
            ByteArrayOutputStream out;
            FileInputStream fis = new FileInputStream(url);
            BufferedInputStream bis = new BufferedInputStream(fis);
            out = new ByteArrayOutputStream();
            @SuppressWarnings("unused")
            int hasRead = 0;
            byte[] buffer = new byte[1024 * 2];
            while ((hasRead = bis.read(buffer)) > 0) {
                // 读出多少数据，向输出流中写入多少
                out.write(buffer);
                out.flush();
            }
            out.close();
            fis.close();
            bis.close();
            byte[] data = out.toByteArray();
            // 长宽减半
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inSampleSize = 3;
            return BitmapFactory.decodeByteArray(data, 0, data.length, opts);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    public void stopPlayVoice() {
        if (voicePlayPosition != -1) {
            View voicePlay = (View) ((Activity) context)
                    .findViewById(voicePlayPosition);
            if (voicePlay != null) {
                if (getItemViewType(voicePlayPosition) == FROM_USER_VOICE) {
                    voicePlay
                            .setBackgroundResource(R.drawable.receiver_voice_node_playing003);
                } else {
                    voicePlay.setBackgroundResource(R.drawable.adj);
                }
            }
            MediaManager.pause();
            voicePlayPosition = -1;
        }
    }


    /**
     * 另外一种方法解析表情将[表情]换成fxxx
     *
     * @param message 传入的需要处理的String
     * @return
     */
    private String convertNormalStringToSpannableString(String message) {
        String hackTxt;
        if (message.startsWith("[") && message.endsWith("]")) {
            hackTxt = message + " ";
        } else {
            hackTxt = message;
        }

        Matcher localMatcher = EMOTION_URL.matcher(hackTxt);
        while (localMatcher.find()) {
            String str2 = localMatcher.group(0);
        }
        return message;
    }


    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE_COUNT;
    }

}